<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

class game extends control {
  public $aPF = array(); //用户在平台方的好友列表

  public function __construct() {
    parent::__construct();

    model::platform()->isAdded(); //应用授权，判断用户是否添加此应用或已登录
    $puid = model::platform()->getUserId(); //获得平台ID
    $this->_checkuser($puid); //当前的cookie的信息不是当前有效的用户的,销毁当前的验证过的信息

    if( $this->uid ) { //用户已登陆,直接加载用户信息
      all::$aU = model::member()->getpbyuid( $this->uid, false );
    }
    if( empty( all::$aU ) ) {
      if (!$puid) {
        func::redirect(model::platform()->apiurl);
      }
      all::$aU = model::member()->getp($puid, model::platform()->pfid, false);
      if (empty(all::$aU)) { //没有注册,注册用户
        $aTU = model::platform()->getUserInfo($puid);
        $uid = model::member()->insert($aTU);
        all::$aU = model::member()->getp($puid, model::platform()->pfid);
        model::gift()->incrLottery(all::$aU['uid'], all::$cfg['ilottery']); //注册送3次抽奖奖励

        $dref = I('_dref'); //注册来源,记录注册来源
        $dref && model::ref()->urefadd(all::$aU['uid'], model::platform()->pfid, $dref);

        $_invite = I('_invite'); //获得邀请ID
        $uid && model::count()->regx(1, $_invite ? true : false); //注册加1

        if( $_invite > 0 ) {
          $flag = model::friend()->setInviteSuc($_invite, all::$aU['puid']); //设置邀请成功
          model::logs()->logmoney($_invite, 8, all::$cfg['invitemoney'], 0, all::$aU['uid'], all::$aU['name'], true); //邀請成功送钱
          model::friend()->add($_invite, all::$aU['uid'], 0); //邀请成功两人互加好友
          model::logs()->sendmsg( all::$aU['uid'], $_invite, L('beinvite') ); //发一条消息给邀请者
          model::gift()->incrLottery($_invite, all::$cfg['ilottery']); //送3次邀请成功奖励
        }

        //已添加应用的好友添加进来
        $this->aPF = model::platform()->getAppFriends();
        count($this->aPF) && model::friend()->addMulti(all::$aU['uid'], $this->aPF, model::platform()->pfid);
      }
    }

    if( empty( all::$aU ) ) { //登陆获取用户信息出错,提示
      func::showerr( '系统错误!' );
    }

    model::member()->getuid(all::$aU['puid'], model::platform()->pfid); //更新缓存里面的uid与puid对应关系

    empty( $this->aPF ) && $this->aPF = model::platform()->getAppFriends();
    if( date( 'Ymd', all::$aU['ltime'] ) != date( 'Ymd', time() ) && $this->aPF ) { //每天更新一次用户的好友信息
      model::friend()->addMulti(all::$aU['uid'], $this->aPF, model::platform()->pfid);
    }
    
    $this->_setlogin(); //设置登陆成功
  }

  public function index() {
    $aOnline = model::online()->get( all::$aU['uid'] );
    if( empty( $aOnline ) ) {
      $aOnline = model::online()->insert( all::$aU['uid'], all::$aU['pfid'] ); //插入到用户在线表并生成mkey
    }
    $aFlash = $this->getSwfVars( all::$aU, $aOnline['mkey'] );
    $aFlash['editurl'] = model::platform()->apiurl . '?m=game&a=edit';
    $aFlash['payurl'] = model::platform()->getpayurl();
    $aFlash['apiurl'] = model::platform()->apiurl;
    if( all::$aU['pfid'] == 4 ) { //fb专用活动参数
      $aFlash['rdswitch'] = all::$cfg['rdswitch']; //猜灯谜
      $aFlash['hbswitch'] = all::$cfg['hbswitch']; //红包活动
      $aFlash['loginswitch'] = all::$cfg['loginswitch']; //连续登陆
    }
    all::$aOut['flashvar'] = http_build_query( $aFlash );

    $aI = model::friend()->getInvite( all::$aU['uid'] ); //获得24小时邀请过的好友,排除掉,不出现在邀请框里面
    $aExclude = $this->aPF;
    foreach( $aI as $k => $v ) {
      ( time() - $v < 24*60*60 ) && $aExclude[] = $k;
    }
    all::$aOut['exclude_ids'] = implode( ',', $aExclude );
    all::$aOut['invited'] = $aExclude;
    func::display( all::$aOut, 'game.htm' );
  }

  public function invite() { //大窗口邀请
    if ( isset( $_REQUEST['ids'] ) ) { //关闭父窗口
      model::friend()->setInvite($this->uid, $_REQUEST['ids']);
      if( model::platform()->pfid == 5 ) {
        model::platform()->sendInvite( $this->puid, $_REQUEST['ids'] );
      }
      echo "<script type='text/javascript'>parent.showinvite();</script>";
    }
    //默认啥也不干
  }

  public function ivtsingle() { //小窗口邀请
    echo "<script type='text/javascript'>parent.clearid('ivtsingle');</script>";
  }

  //更新个人平台资料
  public function edit() {
    $aU = model::platform()->getUserInfo( $this->puid );
    model::member()->update($this->uid, $aU);
    $url = all::$aOut['apiurl'];
    func::redirect( $url, '_top', 'js' );
  }

  public function _gennavvar( $show = 1 ) {
    $aNav = array(
      'show' => $show,
      'url1' => model::platform()->apiurl,
      'url2' => all::$aOut['apiurl'] . '?m=game&a=pay',
      'url3' => 'http://www.facebook.com/apps/application.php?id=' . model::platform()->apiid . '&v=app_2373072738',
      'url4' => 'js://showinvite',
      'hideinvte' => $show != 1 ? 1 : 0,
    );
    all::$aOut['navvar'] = http_build_query( $aNav );
  }

}